# -*-coding:utf-8 -*-

VOLUME_BASE = 100L; #手
AMOUNT_BASE = 100000L; #百元 = 10000分 = 100000厘

class asdict(object):
    def __getitem__(self,key):  #允许dict的调用方法
        #print "getitem:" + key
        if(self.has_key(key)):  
            return getattr(self,key)

    def has_key(self,key):
        return  key in self.__slots__ #如果有继承容器父类，则也需要验证父类的

    def __str__(self):
        """只有以cvs方式保存的时候(测试环境)才用到"""
        return ",".join((str(getattr(self,slot)) for slot in self.__slots__))

    def asdict(self):
        ad = {}
        for slot in self.__slots__:
            ad[slot] = getattr(self,slot)
        return ad

class DatedStock(asdict):
    __slots__ = "none"  #必须设置slots，否则子类即便设置了slots属性，也会存在__dict__

    def __cmp__(self,other):
        #print "in cmp"
        return self.tdate - other.tdate or self.tstock > other.tstock

    def __eq__(self,other): #以便用于Set或者用做dict的key
        #print "in eq"
        return self.tstock == other.tstock and self.tdate == other.tdate

    def __hash__(self):
        #print "in hash"
        return hash(str(self.tstock) + str(self.tdate))

def createQuoteFromSrc(tstock,tdate,topen,tclose,thigh,tlow,tvolume,tamount): #从数据源读取数据创建对象，传入的价格和量的数据都是float型的
    quote = Quote()
    (quote.tstock,quote.tdate,quote.topen,quote.tclose,quote.thigh,quote.tlow,quote.tvolume,quote.tamount) =  (tstock,tdate,f2_milli(topen),f2_milli(tclose),f2_milli(thigh),f2_milli(tlow),tvolume,f2_100(tamount))
    quote.calcAvg()
    return quote

def createQuoteFromDb(tstock,tdate,topen,tclose,thigh,tlow,tvolume,tamount,tavg): #从数据表读取数据创建对象,传入参数都已经是1/1000为单位的整数
    quote = Quote()
    (quote.tstock,quote.tdate,quote.topen,quote.tclose,quote.thigh,quote.tlow,quote.tvolume,quote.tamount,quote.tavg) =  (tstock,tdate,topen,tclose,thigh,tlow,tvolume,tamount,tavg)
    return quote

xunitbase = 1000

class Quote(DatedStock):  #在属性名字前加t的目的是在保证属性名和字段名同名的同时，避免与数据库的关键字冲突
    """行情数据: 价格单位都是厘,成交量为手,成交金额为百元"""
    __slots__ = 'tstock','tdate','topen','tclose','thigh','tlow','tavg','tvolume','tamount'

    def __init__(self):
        #super(DatedStock,self).__init__(self)
        self.tstock = "AAAAAA"
        self.tdate = 99999999
        self.topen = self.tclose = self.thigh = self.tlow = self.tavg = self.tvolume = self.tamount = 0

    def calcAvg(self):
        if self.tvolume != 0:
            #先将成交金额单位调整为厘(*100000)，然后去除以成交量，得到以厘为单位均价
            self.tavg = int((self.tamount * AMOUNT_BASE + self.tvolume * VOLUME_BASE/2)/(self.tvolume*VOLUME_BASE))
        else:
	        self.tavg = self.tclose;
 
    def to_tuple(self):
        return self.tstock,self.tdate,self.topen,self.tclose,self.thigh,self.tlow,self.tavg,self.tvolume,self.tamount


def createXInfoFromSrc(tstock,tregisterday,texecuteday,tpgbl,tsgbl,tfhbl,tpgj,tzfs=0,tzfj=0):
    #从数据源读取数据创建对象，传入的价格和量的数据都是float型的
    xinfo = XInfo()
    (xinfo.tstock,xinfo.tregisterday,xinfo.texecuteday,xinfo.tpgbl,xinfo.tsgbl,xinfo.tfhbl,xinfo.tpgj,xinfo.tzfs,xinfo.tzfj) =  (tstock,tregisterday,texecuteday,f2_milli(tpgbl),f2_milli(tsgbl),f2_milli(tfhbl),f2_milli(tpgj),f2_milli(tzfs),f2_milli(tzfj))
    return xinfo

def createXInfoFromDb(id,tstock,tregisterday,texecuteday,tpgbl,tsgbl,tfhbl,tpgj,tzfs,tzfj,tstate,tremark):
    #从数据表读取数据创建对象,传入参数都已经是1/1000为单位的整数
    xinfo = XInfo()
    (xinfo.id,xinfo.tstock,xinfo.tregisterday,xinfo.texecuteday,xinfo.tpgbl,xinfo.tsgbl,xinfo.tfhbl,xinfo.tpgj,xinfo.tzfs,xinfo.tzfj,xinfo.tstate,xinfo.tremark) =  (id,tstock,tregisterday,texecuteday,tpgbl,tsgbl,tfhbl,tpgj,tzfs,tzfj,tstate,tremark)
    return xinfo

class XInfo(DatedStock): #除权信息
    __slots__="id",'tstock','tsgbl','tpgbl','tfhbl','tpgj','tzfs','tzfj','tjjs','tregisterday','texecuteday','tstate','tremark'

    def __init__(self):
        self.id = 0
        self.tstock = "AAAAAA"
        self.tregisterday = self.texecuteday = 99999999
        self.tremark = ""
        self.tstate = 0;    #未除权
        self.tsgbl = self.tpgbl = self.tfhbl = self.tpgj = self.tzfs = self.tzfj = self.tjjs = 0

    def __getattr__(self,name): #以便DateFilter
        if(name == "tdate"):
            return self.texecuteday

def createReportFromSrc(tstock,ttype,treleaseday,tdate,tzgb,tbg,thg,tag,tmgwfplr,tmgsy,tmgjzc,tmggjj):
    #从数据源读取数据创建对象，传入的价格和量的数据都是float型的
    report = Report()
    (report.tstock,report.ttype,report.treleaseday,report.tdate,report.tzgb,report.tbg,report.thg,report.tag,report.tmgwfplr,report.tmgsy,report.tmgjzc,report.tmggjj) = (tstock,ttype,treleaseday,tdate,int(tzgb),int(tbg),int(thg),int(tag),f2_milli(tmgwfplr),f2_milli(tmgsy),f2_milli(tmgjzc),f2_milli(tmggjj))
    #assert report.zgb < 2000000000
    return report

def createReportFromDb(tstock,ttype,treleaseday,tdate,tzgb,tbg,thg,tag,tmgwfplr,tmgsy,tmgjzc,tmggjj,tremark):
    #从数据表读取数据创建对象,传入参数都已经调整完毕
    report = Report()
    (report.tstock,report.ttype,report.treleaseday,report.tdate,report.tzgb,report.tbg,report.thg,report.tag,report.tmgwfplr,report.tmgsy,report.tmgjzc,report.tmggjj,report.tremark) = (tstock,ttype,treleaseday,tdate,tzgb,tbg,thg,tag,tmgwfplr,tmgsy,tmgjzc,tmggjj,tremark)
    return report

class Report(DatedStock): #报表信息
    __slots__= 'tstock','ttype','treleaseday','tzgb','tbg','thg','tag','tmgwfplr','tmgsy','tmgjzc','tmggjj','tremark'
    
    def __init__(self):
        self.tstock = "AAAAAA"
        self.ttype = 1 #1年2半年3季4月 0未知
        self.tremark = ""
        self.treleaseday = 99999999
        self.tdate = 9999
        self.tzgb = self.tbg = self.thg = self.tag = self.tmgwfplr = self.tmgsy = self.tmgjzc = self.tmggjj = 0

    def __getattr__(self,name): #以便DateFilter
        if(name == "tdate"):
            print self.treleaseday//100
            return self.treleaseday//100

def f2_milli(fsrc):    # float转换为0.001为单位的整数
    return int(fsrc*1000 + 0.5)

def f2_centi(fsrc):   # float转换为0.01为单位的整数
    return int(fsrc * 100 + 0.5)

def f2_100(fsrc):   # float转换为100为单位的整数
    return int(fsrc/100.0 + 0.5)

def f2_10000(fsrc):   # float转换为100为单位的整数
    return int(fsrc/10000.0 + 0.5)

class DataObject(object):pass
